﻿Imports System.IO
Imports System.Runtime.Serialization

Imports IntoNails.Models

<Serializable()> _
Public Class EmployeeRepository
    Inherits AbstractFileRepository(Of Employee)
    Implements IEmployeeRepository, ISerializable

#Region "Constructors"

    Public Sub New(ByVal dataFile As String)
        Me.DataFile = dataFile
        Me.Load()
    End Sub

    Public Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
        Me.InnerDataList = DirectCast(info.GetValue("EmployeeList", GetType(List(Of Employee))), List(Of Employee))
    End Sub

    Public Sub GetObjectData(ByVal info As SerializationInfo, ByVal ctxt As StreamingContext) _
        Implements ISerializable.GetObjectData
        info.AddValue("EmployeeList", Me.InnerDataList)
    End Sub

#End Region

    Public Function Add(ByVal data As Employee) As Boolean _
        Implements IDataRepository(Of Employee).Add

        If Not Me.InnerDataList.Contains(data) Then
            Me.InnerDataList.Add(data)
            Return True
        End If

        Return False
    End Function

    Public Function GetItem(ByVal uniqueCode As Integer) As Employee _
        Implements IDataRepository(Of Employee).GetItem

        Return Me.InnerDataList.Where(Function(e) e.UniqueCode = uniqueCode).FirstOrDefault()

    End Function

    Public Function GetRawCollection() As System.Collections.Generic.List(Of Employee) _
        Implements IDataRepository(Of Employee).GetRawCollection

        Return Me.InnerDataList

    End Function

    Public Function Remove(ByVal uniqueCode As Integer) As Boolean _
        Implements IDataRepository(Of Employee).Remove

        Dim removed As Employee = Me.GetItem(uniqueCode)

        If removed IsNot Nothing Then
            removed.IsValid = False

            removed.DateChanged = Date.Today
            Return True
        End If

        Return False
    End Function

    Public Function Update(ByVal uniqueCode As Integer, ByVal newData As Employee) As Boolean _
        Implements IDataRepository(Of Employee).Update

        Dim e As Employee = Me.GetItem(uniqueCode)
        If e Is Nothing Then
            Return False
        End If

        If newData Is Nothing Or newData.UniqueCode <> e.UniqueCode Then
            Return False
        End If

        With e
            e.FullName = newData.FullName
            e.BirthDate = newData.BirthDate
            e.Address = newData.Address
            e.ContactNumber = newData.ContactNumber
            e.CellPhone = newData.CellPhone
            e.IsValid = newData.IsValid
            e.DateChanged = Date.Today
        End With

        Return True
    End Function

    Public Function GetValidEmployeeCollection() As List(Of Employee) _
        Implements IEmployeeRepository.GetValidEmployeeCollection

        Return Me.InnerDataList.Where(Function(e) e.IsValid = True).ToList()

    End Function

#Region "RepositoryBase Overriden"

    Public Overrides Sub Load()
        If IO.File.Exists(Me.DataFile) Then
            Using readStream As New FileStream(Me.DataFile, FileMode.Open, FileAccess.Read)
                Me.InnerDataList = readStream.Deserialize(Of List(Of Employee))()
            End Using
        Else
            Me.InnerDataList = New List(Of Employee)()
        End If
    End Sub

    Public Overrides Sub Save()
        Using writeStream As New FileStream(Me.DataFile, FileMode.OpenOrCreate, FileAccess.Write)
            Me.InnerDataList.Serialize(writeStream)
        End Using
    End Sub

#End Region

End Class
